package com.pm.business.controller;

import java.util.List;
import java.util.stream.Collectors;
import javax.servlet.http.HttpServletResponse;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import com.pm.common.annotation.Log;
import com.pm.common.core.controller.BaseController;
import com.pm.common.core.domain.AjaxResult;
import com.pm.common.enums.BusinessType;
import com.pm.business.domain.BidProjectWorkflow;
import com.pm.business.service.IBidProjectWorkflowService;
import com.pm.common.utils.poi.ExcelUtil;
import com.pm.common.core.page.TableDataInfo;

/**
 * 投标进程管理Controller
 * 
 * @author zxl
 * @date 2022-05-10
 */
@RestController
@RequestMapping("/business/bid_project_workflow")
public class BidProjectWorkflowController extends BaseController
{
    @Autowired
    private IBidProjectWorkflowService bidProjectWorkflowService;

    /**
     * 查询投标进程管理列表
     */
    @PreAuthorize("@ss.hasPermi('business:bid_project_workflow:list')")
    @GetMapping("/list")
    public TableDataInfo list(BidProjectWorkflow bidProjectWorkflow)
    {
        startPage();
        List<BidProjectWorkflow> list = bidProjectWorkflowService.selectBidProjectWorkflowList(bidProjectWorkflow);
        if(list!=null && list.size()>0){
            List<Long> idList = list.stream().map(BidProjectWorkflow::getWorkflowId).collect(Collectors.toList());
            list = bidProjectWorkflowService.selectBidProjectWorkflowDetailList(idList);

        }
        return getDataTable(list);
    }

    /**
     * 导出投标进程管理列表
     */
    @PreAuthorize("@ss.hasPermi('business:bid_project_workflow:export')")
    @Log(title = "投标进程管理", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(HttpServletResponse response, BidProjectWorkflow bidProjectWorkflow)
    {
        List<BidProjectWorkflow> list = bidProjectWorkflowService.selectBidProjectWorkflowList(bidProjectWorkflow);
        ExcelUtil<BidProjectWorkflow> util = new ExcelUtil<BidProjectWorkflow>(BidProjectWorkflow.class);
        util.exportExcel(response, list, "投标进程管理数据");
    }

    /**
     * 获取投标进程管理详细信息
     */
    @PreAuthorize("@ss.hasPermi('business:bid_project_workflow:query')")
    @GetMapping(value = "/{workflowId}")
    public AjaxResult getInfo(@PathVariable("workflowId") Long workflowId)
    {
        return AjaxResult.success(bidProjectWorkflowService.selectBidProjectWorkflowByWorkflowId(workflowId));
    }

    @GetMapping(value = "/getActiveWorkflow")
    public AjaxResult getActiveWorkflow()
    {
        return AjaxResult.success(bidProjectWorkflowService.selectActiveWorkflow());
    }

    /**
     * 新增投标进程管理
     */
    @PreAuthorize("@ss.hasPermi('business:bid_project_workflow:add')")
    @Log(title = "投标进程管理", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody BidProjectWorkflow bidProjectWorkflow)
    {
        if("1".equals(bidProjectWorkflow.getWorkflowStatus()) && bidProjectWorkflowService.countActiveWorkflow(new BidProjectWorkflow())>0){
            return error("全局只能有一个启用的工作流");
        }
        return toAjax(bidProjectWorkflowService.insertBidProjectWorkflow(bidProjectWorkflow));
    }

    /**
     * 修改投标进程管理
     */
    @PreAuthorize("@ss.hasPermi('business:bid_project_workflow:edit')")
    @Log(title = "投标进程管理", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody BidProjectWorkflow bidProjectWorkflow)
    {
        if("1".equals(bidProjectWorkflow.getWorkflowStatus()) && bidProjectWorkflowService.countActiveWorkflow(bidProjectWorkflow)>0){
            return error("全局只能有一个启用的工作流");
        }
        return toAjax(bidProjectWorkflowService.updateBidProjectWorkflow(bidProjectWorkflow));
    }

    /**
     * 删除投标进程管理
     */
    @PreAuthorize("@ss.hasPermi('business:bid_project_workflow:remove')")
    @Log(title = "投标进程管理", businessType = BusinessType.DELETE)
	@DeleteMapping("/{workflowIds}")
    public AjaxResult remove(@PathVariable Long[] workflowIds)
    {
        return toAjax(bidProjectWorkflowService.deleteBidProjectWorkflowByWorkflowIds(workflowIds));
    }
}
